/**
 * 给定一个 n × n 的二维矩阵表示一个图像。
 * <p>
 * 将图像顺时针旋转 90 度。
 * <p>
 * 说明：
 * <p>
 * 你必须在原地旋转图像，这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。
 * <p>
 * 示例 1:
 * <p>
 * 给定 matrix =
 * [
 * [1,2,3],
 * [4,5,6],
 * [7,8,9]
 * ],
 * <p>
 * 原地旋转输入矩阵，使其变为:
 * [
 * [7,4,1],
 * [8,5,2],
 * [9,6,3]
 * ]
 * 示例 2:
 * <p>
 * 给定 matrix =
 * [
 * [ 5, 1, 9,11],
 * [ 2, 4, 8,10],
 * [13, 3, 6, 7],
 * [15,14,12,16]
 * ],
 * <p>
 * 原地旋转输入矩阵，使其变为:
 * [
 * [15,13, 2, 5],
 * [14, 3, 4, 1],
 * [12, 6, 8, 9],
 * [16, 7,10,11]
 * ]
 */
class Solution {

    public static void main(String[] args) {
        /*int[][] matrix = new int[3][3];
        int[] a1 = new int[]{1, 2, 3};
        int[] a2 = new int[]{4, 5, 6};
        int[] a3 = new int[]{7, 8, 9};
        matrix[0] = a1;
        matrix[1] = a2;
        matrix[2] = a3;
        rotate(matrix);
        System.out.println(matrix.length);*/

        int[][] matrix = new int[4][4];
        int[] a1 = new int[]{5, 1, 9, 11};
        int[] a2 = new int[]{2, 4, 8, 10};
        int[] a3 = new int[]{13, 3, 6, 7};
        int[] a4 = new int[]{15, 14, 12, 16};
        matrix[0] = a1;
        matrix[1] = a2;
        matrix[2] = a3;
        matrix[3] = a4;
        rotate(matrix);
        System.out.println(matrix.length);

    }

    public static void rotate(int[][] matrix) {
        int all = matrix[0].length - 1;
        for (int len = matrix[0].length, count = 0; len >= 2; len -= 2, count++) {
            // 拷贝最初的数据
            int[] copy = new int[len];
            System.arraycopy(matrix[count], count, copy, 0, len);
            for (int i = 0; i < len - 1; i++) {
                matrix[count][all - count - i] = matrix[count + i][count];
            }
            for (int i = 0; i < len - 1; i++) {
                matrix[count + i][count] = matrix[all - count][count + i];
            }
            for (int i = 0; i < len - 1; i++) {
                matrix[all - count][count + i] = matrix[all - count - i][all - count];
            }
            for (int i = 0; i < len; i++) {
                matrix[count + i][all - count] = copy[i];
            }
        }
    }
}